import { ElMessage, ElMessageBox } from "element-plus";
import type { HandleData } from "./interface";

/**
 * @description 操作单条数据信息 (二次确认【删除、禁用、启用、重置密码】)
 * @param {Function} api 操作数据接口的api方法 (必传)
 * @param {String} message 提示信息 (必传)
 * @param {String} confirmType icon类型 (不必传,默认为 warning)
 * @returns {Promise}
 */
export function useHandleData(
  api: (params: any) => Promise<any> | any,
  message: string,
  confirmType: HandleData.MessageType = "warning"
) {
  return new Promise((resolve) => {
    ElMessageBox.confirm(`是否${message}?`, "温馨提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: confirmType,
      draggable: true,
      buttonSize: "default",
    })
      .then(async () => {
        await api().then((res) => {
          resolve(res);
        });

        ElMessage({
          type: "success",
          message: `${message}成功!`,
        });
        resolve(res);
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "取消操作",
        });
      });
  });
}
